package chances.epg.data.taglib;

import java.util.List;

import javax.servlet.jsp.JspException;

import org.slf4j.Logger;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.entity.content.Channel;
import chances.epg.entity.content.Link;
import chances.epg.entity.content.SubjectArea;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.context.AccessContextUtils;

/**
 * 专题页面热点列表.
 * 
 * @author Elysium
 *
 */
public class SubjectAreaTag extends QueryListTag {
	private static final Logger logger = EpgLogFactory.getSystemLogger();

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -7257888531224648124L;

	private String code;

	@Override
	public int doEndTag() throws JspException {
		try {
			List<SubjectArea> result = this.getContentService().getSubjectAreas(this.code);
			// set access url
			AccessContext accessContext = AccessContextUtils.getAccessContext(pageContext);
			for (SubjectArea subjectArea : result) {
				subjectArea.setUrl(this.getAreaItemUrl(accessContext, subjectArea));
			}
			this.setCollectionResult(result);
		} catch (Throwable ex) {
			logger.error("SubjectAreaTag error", ex);
		}
		return super.doEndTag();
	}

	private String getAreaItemUrl(AccessContext context, SubjectArea subjectArea) {
		String url = "";
		if (subjectArea.getObjType().equals(SubjectArea.OBJTYPE_CHANNEL)) {
			Channel channel = this.getChannelService().getChannelByCode(subjectArea.getObjCode());
			url = this.navigateUrlFactory.generatePlayUrl(context, channel);
			pageContext.setAttribute("channelNum", channel.getChannelNum());
		} else if(subjectArea.getObjType().equals(SubjectArea.OBJTYPE_LINK)) {
			Link link = this.getContentService().getLinkByCode(subjectArea.getObjCode());
			url = this.navigateUrlFactory.generateLinkUrl(context, link);
		} else {
			url = navigateUrlFactory.generateURL(context, subjectArea);
		}
		return url;
	}
	

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
